<?php
namespace common\services;

use common\base\BaseActiveRecord;
use common\helpers\TStringHelper;
use common\models\ExhibitionCategory;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;

class ExhibitionCategoryServices extends ExhibitionCategory
{

    /**
     * @param null $parentId
     * @return ActiveDataProvider
     */
    public function getList($parentId = null, $defaultPageSize = 15){
        $model = ExhibitionCategory::find(false);
        if (!empty($parentId) ) {
            $model->andFilterWhere(['=', 'parent_id', $parentId]);
        }
        $model->addOrderBy('order desc');
        $dataProvider = new ActiveDataProvider([
            'query' => $model,
            'pagination' => [
                'pagesize' => $defaultPageSize,
            ]
        ]);
        return $dataProvider;
    }

    /**
     * 分类树
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getData($parentId = null){
        $model = ExhibitionCategory::find(false);
        $model->andFilterWhere(['=', 'status', BaseActiveRecord::STATUS_FLAG_NORMAL])
            ->addOrderBy([
                'id' => SORT_ASC,
                'order' => SORT_DESC
            ]);
        if (!empty($parentId)){
            $model->andFilterWhere(['=', 'parent_id', $parentId]);
        }

        $list = $model->select('id,name,parent_id')->asArray()->all();
        $result = array();
        if (!empty($list)){
            foreach ($list as $item){
                if ($item['parent_id'] == 0){
                    $result['parent'][] = $item;
                }else{
                    $result['children'][$item['parent_id']][] = $item;
                }
            }
        }
        return $result;
    }

    /**
     * 获取分类树
     * @param $data
     * @param bool $addBtn
     * @param bool $editBtn
     * @param bool $deleteBtn
     * @return string
     */
    public function getTree($data){
        $category_directory = \Yii::t('common', 'category_directory');
        $html = "<ul>";
        $html .= "<li class=\"jstree-open\" id='0' data-jstree='{\"opened\":true,\"selected\":true,\"icon\":\"glyphicon glyphicon-duplicate\"}'>$category_directory";
        if (!empty($data['parent'])){
            $html .= "<ul>";
            foreach ($data['parent'] as $item) {
                $html .= "<li id=\"" . $item['id'] . "\" data-jstree='{\"icon\":\"glyphicon glyphicon-duplicate\"}'>" . $item['name'];
                if (!empty($data['children'][$item['id']])) {
                    $html .= "<ul>";
                    foreach ($data['children'][$item['id']] as $v) {
                        $html .= "<li id=\"" . $v['id'] . "\" data-jstree='{\"icon\":\"glyphicon glyphicon-duplicate\"}'>" . $v['name'];
                        if (!empty($data['children'][$v['id']])) {
                            $html .= "<ul>";
                            foreach ($data['children'][$v['id']] as $vv) {
                                $html .= "<li id=\"" . $vv['id'] . "\" data-jstree='{\"icon\":\"glyphicon glyphicon-duplicate\"}'>" . $vv['name'] . "</li>";
                            }
                            $html .= "</ul>";
                        }
                        $html .= "</li>";
                    }
                    $html .= "</ul>";
                }
                $html .= "</li>";
            }
            $html .= "</ul>";
        }
        $html .= "</li>";
        $html .= "</ul>";
        return $html;
    }

    /**
     * @param $parentId
     * @return int|string
     */
    public function getChildCount($parentId = null){
        $model = ExhibitionCategory::find(false);
        if (!empty($parentId)) {
            $model->andFilterWhere(['=', 'parent_id', $parentId]);
        }

        return $model->count(1);
    }

    /**
     * 返回所有分类数据
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getIndexCategory($params = array()){
        $model = ExhibitionCategory::find(false);
        $model->andFilterWhere(['=', 'status', self::STATUS_FLAG_NORMAL])
            ->orderBy('order desc,id asc')
            ->select("id,name");

        if (isset($params['is_index']) && $params['is_index'] != '') {
            $model->andFilterWhere(['=', 'is_index', intval($params['is_index'])]);
        }
        if (isset($params['limit'])) {
            $model->limit(intval($params['limit']));
        }

        $result = $model->asArray()->all();

        return $result;
    }

    /**
     * 获取分类
     * @param $categoryId
     * @param bool $withCache
     * @return array|mixed|null
     */
    public function getDataByCategoryId($categoryId, $withCache = true) {
        $cacheKey = 'exhibition_category_by_'.$categoryId;
        $result = $withCache ? \Yii::$app->cache->get($cacheKey) : null;
        if (empty($result)) {
            $model = ExhibitionCategory::findOne($categoryId);
            $result = $model ? $model->toArray(['id', 'name']) : null;
            if ($withCache && !empty($result)) {
                \Yii::$app->cache->set($cacheKey, $result);
            }
        }
        return $result;
    }

    /**
     * 根据行业标题查询行业信息
     * @param $title
     * @return array|bool|null
     */
    public function getCategoryByTitle($title) {
        if (empty($title)) return false;
        $model = ExhibitionCategory::findOne(['status' => ExhibitionCategory::STATUS_FLAG_NORMAL, 'name' => $title]);

        return $model ? $model->toArray() : null;
    }

}
